/**
 * DateCalendarConverter
 *
 * @author Chris Pratt
 *
 * 12/16/2011
 */
package com.anodyzed.onyx.type;

import java.util.Calendar;
import java.util.Date;

public class DateCalendarConverter implements Converter {

  /**
   * Convert Strings (or CharSequence's) to/from Enum's
   *
   * @param from Either a CharSequence or an Enum 
   * @param to The type to convert to 
   * @return Either an Enum or a String
   */
  @Override
  public <T> T convert (Object from,Class<T> to) {
    if(from instanceof Date) {
      Calendar cal = Calendar.getInstance();
      cal.setTime((Date)from);
      return to.cast(cal);
    } else if(from instanceof Calendar) {
      return to.cast(((Calendar)from).getTime());
    }
    throw new ConversionException("{0.class.name} is neither a Date nor a Calendar",from);
  } //convert

} //*DateCalendarConverter
